package com.tian.clothing.network.asynctask;

import android.os.AsyncTask;

/**
 * @ClassName: BaseAsyncTask
 * @Description: TODO
 * @author: dreamfutureba@gmail.com
 * @date: 2013年11月21日 下午3:23:21
 * @param <Params>
 * @param <Progress>
 * @param <Result>
 */
public abstract class BaseAsyncTask<Params,Progress,Result> extends AsyncTask<Params, Progress, Result> {
	private static final String TAG=BaseAsyncTask.class.getSimpleName();
	
	public BaseAsyncTask() {
		super();
	}
	private Exception exception;

	@Override
	protected void onPreExecute() {
		onBeforeTask();
	}

	@Override
	protected Result doInBackground(Params... params) {
		try {
			return onTask(params);
		} catch (Exception e) {
			exception = e;
			return null;
		}
	}

	@Override
	protected void onPostExecute(Result result) {
		if (hasException()) {
			onTaskError(exception);
		} else {
			onAfterTask(result);
		}
	}

	private boolean hasException() {
		return exception != null;
	}
	
	abstract void onBeforeTask();

	abstract Result onTask(Params... params) throws Exception;

	abstract void onAfterTask(Result result);

	abstract void onTaskError(Exception exception);

}
